package com.mingqi.enetconnected.view.Dialog.single;


import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;

import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.util.date.DateInfo;
import com.mingqi.enetconnected.util.date.DateTimeInfo;
import com.mingqi.enetconnected.util.resource.ResourceUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by li on 2018/8/31.
 * WeChat 18571658038
 * author LiWei
 */

public class SinglePickerTimeEmpAdapter extends BaseAdapter {


    private List<DateInfo> mData;
    private ListView listView;


    public SinglePickerTimeEmpAdapter(ListView listView, List<DateInfo> mData) {
        if (mData == null) {
            mData = new ArrayList<>();
        }
        this.mData = mData;
        this.listView = listView;

    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        MySingleHolder singleHolder = null;
        if (convertView == null) {

            convertView = ResourceUtils.getViewFollowParent(R.layout.item_single_choose_emp_time, parent);
            singleHolder = new MySingleHolder();
            singleHolder.cbSingle = convertView.findViewById(R.id.cb_single);
            singleHolder.textView = convertView.findViewById(R.id.choose_other_time);
            singleHolder.gridView = convertView.findViewById(R.id.gridview_layout);
            singleHolder.empGridView = convertView.findViewById(R.id.emp_gridview_layout);
            singleHolder.empTextView = convertView.findViewById(R.id.choose_other_emp);
            convertView.setTag(singleHolder);

        } else {

            singleHolder = (MySingleHolder) convertView.getTag();
        }

        singleHolder.cbSingle.setText(mData.get(position).getValue());


        if (listView.getCheckedItemPosition() == position) {

            singleHolder.gridView.setVisibility(View.VISIBLE);
            singleHolder.textView.setVisibility(View.VISIBLE);
            singleHolder.empGridView.setVisibility(View.VISIBLE);
            singleHolder.empTextView.setVisibility(View.VISIBLE);

            final GridViewAdapter adapter = mData.get(position).getAdapter();
            singleHolder.gridView.setAdapter(adapter);

            singleHolder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    DateTimeInfo dateTimeInfo = (DateTimeInfo) adapter.getItem(position);
                    if (!dateTimeInfo.isEnable()) {
                        return;
                    }
                    if (dateTimeInfo.isChecked()) {
                        dateTimeInfo.setChecked(false);
                        adapter.setCurrentPosition(ListView.INVALID_POSITION);
                        adapter.notifyDataSetChanged();
                        return;
                    }

                    adapter.selectValue(position);

                }
            });
            final GridViewAdapter empAdapter = mData.get(position).getEmpAdapter();
            singleHolder.empGridView.setAdapter(empAdapter);

            singleHolder.empGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    DateTimeInfo dateTimeInfo = (DateTimeInfo) empAdapter.getItem(position);
                    if (!dateTimeInfo.isEnable()) {
                        return;
                    }
                    if (dateTimeInfo.isChecked()) {
                        empAdapter.setCurrentPosition(ListView.INVALID_POSITION);
                        dateTimeInfo.setChecked(false);
                        empAdapter.notifyDataSetChanged();
                        return;
                    }

                    empAdapter.selectValue(position);

                }
            });


        } else {
            singleHolder.textView.setVisibility(View.GONE);
            singleHolder.gridView.setVisibility(View.GONE);
            singleHolder.empGridView.setVisibility(View.GONE);
            singleHolder.empTextView.setVisibility(View.GONE);
        }

        return convertView;
    }

    public void refresh(List<DateInfo> data) {
        if (data == null) {
            data = new ArrayList<>();
        }
        mData = data;
        notifyDataSetChanged();
    }

    static class MySingleHolder {
        CheckBox cbSingle;
        TextView textView;
        GridView gridView;
        GridView empGridView;
        TextView empTextView;
    }

}
